<template>
  <div class="mask" @click="clickMask" v-if="showMask"></div>
  <Layout ref="container">
    <div ref="mainContainer" class="main-container" v-loading="isLoading">
      <BlogDetail :blog="data" v-if="data" />
      <BlogCommentNew v-if="!isLoading" :articleId="data?.id" />
      <!-- <BlogComment v-if="!isLoading" /> -->
    </div>
    <template #right>
      <div class="right-container" :class="{ have: !isShow }" v-loading="isLoading">
        <BlogTOC :toc="data.toc" v-if="data" @select="handleSelect" />
      </div>
    </template>
  </Layout>
  <div>
    <Teleport to="body">
      <div
        class="muluBtn"
        :class="{ remove: !isShow }"
        @click="handleClick"
        ref="muluBtn"
        @touchmove="touchmove($event)"
        @touchstart="touchstart($event)"
        @touchend="touchend($event)"
      >
        目录
      </div>
    </Teleport>
  </div>
</template>

<script setup lang="ts">
import { titleController } from '@/utils'
import mainScroll from '@/composition/useMainScroll'
import { getBlog } from '@/api/blog'
import BlogDetail from './components/BlogDetail.vue'
import BlogTOC from './components/BlogTOC.vue'
// import BlogComment from './components/BlogComment.vue'
import BlogCommentNew from './components/BlogCommentNew.vue'
import type { getBlogType } from '@/types/blog'
import { useRouter } from 'vue-router'
import { getCurrentInstance, onMounted, ref, type ComponentPublicInstance } from 'vue'
const proxy: ComponentPublicInstance<any> = getCurrentInstance()?.proxy
const router = useRouter()
const isLoading = ref(false)
const data = ref<getBlogType>()
const muluBtn = ref<any>(null)
const container = ref<any>(null)
const disX = ref(0)
const disY = ref(0)
onMounted(async () => {
  isLoading.value = true
  let resp = await getBlog(router.currentRoute.value.params.id as string)
  data.value = resp
  isLoading.value = false
  if (!resp) {
    // 文章不存在
    router.push('/404')
    return
  }
  titleController.setRouteTitle(resp.title)
})
const showMask = ref(false)
const isShow = ref(true)
const clickMask = () => {
  showMask.value = false
  isShow.value = true
}
mainScroll(proxy, 'mainContainer')

const handleClick = function () {
  isShow.value = false
  showMask.value = true
}
const handleSelect = function () {
  isShow.value = true
  showMask.value = false
}
// 手指落下时触发
function touchstart(event: any) {
  const rect = muluBtn.value.getBoundingClientRect()
  disX.value = event.targetTouches[0].clientX - rect.left
  disY.value = event.targetTouches[0].clientY - rect.top
}

// 手指移动时触发
function touchmove(event: any) {
  // 2，获取手指移动的实时位置  需要减去位置差
  let moveX = event.targetTouches[0].clientX - disX.value
  let moveY = event.targetTouches[0].clientY - disY.value
  // 5，开始移动
  muluBtn.value.style.left = moveX + 'px'
  muluBtn.value.style.top = moveY + 'px'
}
function touchend(event: any) {
  if (event.changedTouches[0].clientX < container.value.$el.clientWidth / 2) {
    muluBtn.value.style.left = 5 + 'px'
  } else {
    const width = muluBtn.value.getBoundingClientRect().width
    muluBtn.value.style.left = container.value.$el.clientWidth - width - 5 + 'px'
  }
}
</script>

<style scoped lang="scss">
.main-container {
  overflow-y: scroll;
  height: 100%;
  box-sizing: border-box;
  padding: 20px;
  position: relative;
  width: 100%;
  overflow-x: hidden;
  scroll-behavior: smooth;
}

.right-container {
  max-width: 250px;
  min-width: 200px;
  height: 100%;
  overflow-y: scroll;
  box-sizing: border-box;
  position: relative;
  padding: 20px;
  background-color: #f6f6f6;

  @media screen and (max-width: 992px) {
    // padding: 0;
    // overflow: hidden;
    display: none;
    position: absolute;
    right: 0;
  }
}

.blog-toc-mobile {
  // display: none;
  @media screen and (max-width: 992px) {
    display: block;
    height: 100vh;
    overflow-y: scroll;
    top: 0;
    left: -50%;
    position: absolute;
    transition: all 0.3s ease;
  }
}

.muluBtn {
  display: none;

  @media screen and (max-width: 992px) {
    display: block;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    top: 60%;
    right: 5px;
    border-radius: 50%;
    position: absolute;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.55);
  }
}

.remove {
  display: none;
}

.tocMobileleft {
  left: 0;
}

.mask {
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.55);
  position: absolute;
  z-index: 7;
}

.have {
  display: block;
  z-index: 9;
  background-color: #fff;
  width: 150px;
}
</style>
